package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.domain.req.DcsRealTimeDataReq;
import com.ruibang.glass.produce.domain.resp.dcs.td.*;
import com.ruibang.glass.produce.entity.DefectDirectly;
import com.ruibang.glass.produce.mapper.DcsTdRealTimeDataMapper;
import com.ruibang.glass.produce.service.DcsTdRealTimeDataService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-04-23  14:57
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class DcsTdRealTimeDataServiceImpl implements DcsTdRealTimeDataService {


    @Autowired
    private DcsTdRealTimeDataMapper dcsTdRealTimeDataMapper;

    /**
     * @Description:澄清段实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<ClarificationSection> queryClarificationSection(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryClarificationSection(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:降温段实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<CoolingSection> queryCoolingSection(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryCoolingSection(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:搅拌桶实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<MixingDrum> queryMixingDrum(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryMixingDrum(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:卸料口实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<DischargePort> queryDischargePort(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryDischargePort(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:冷却段实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<CoolingSegment> queryCoolingSegment(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryCoolingSegment(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:供料桶实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<FeedBucket> queryFeedBucket(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryFeedBucket(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:搅拌机参数实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<MixerParam> queryMixerParam(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryMixerParam(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    /**
     * @Description:冷却系统实时数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-24 15:59
     */
    @Override
    public IPage<CoolingSystem> queryCoolingSystem(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认默认查询时间
        setDefaultTime(dcsRealTimeDataReq);
        return dcsTdRealTimeDataMapper.queryCoolingSystem(new Page<>(dcsRealTimeDataReq.getPageNo(), dcsRealTimeDataReq.getPageSize()), dcsRealTimeDataReq);
    }


    @Override
    public void exportClarificationSection(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认间隔时间60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<ClarificationSection> page = this.queryClarificationSection(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), ClarificationSection.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportCoolingSection(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<CoolingSection> page = this.queryCoolingSection(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), CoolingSection.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportMixingDrum(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<MixingDrum> page = this.queryMixingDrum(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), MixingDrum.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportDischargePort(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<DischargePort> page = this.queryDischargePort(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), DischargePort.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportCoolingSegment(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<CoolingSegment> page = this.queryCoolingSegment(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), CoolingSegment.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportFeedBucket(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<FeedBucket> page = this.queryFeedBucket(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), FeedBucket.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportMixerParam(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //导出
        IPage<MixerParam> page = this.queryMixerParam(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), MixerParam.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void exportCoolingSystem(DcsRealTimeDataReq dcsRealTimeDataReq, HttpServletResponse response) {
        dcsRealTimeDataReq.setPageNo(-1);
        dcsRealTimeDataReq.setPageSize(-1);
        //默认频率为60分钟
        if (dcsRealTimeDataReq.getInterval() == null || dcsRealTimeDataReq.getInterval() <= 0) {
            dcsRealTimeDataReq.setInterval(60);
        }
        //导出
        IPage<CoolingSystem> page = this.queryCoolingSystem(dcsRealTimeDataReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), CoolingSystem.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public HeatingTrendChart heatingTrendChart(DcsRealTimeDataReq dcsRealTimeDataReq) {
        //默认查询3天
        if (dcsRealTimeDataReq.getEndDate() == null && dcsRealTimeDataReq.getStartDate() == null) {
            LocalDate endDate = LocalDate.now();
            LocalDate startDate = endDate.minusDays(3);
            dcsRealTimeDataReq.setStartDate(startDate);
            dcsRealTimeDataReq.setEndDate(endDate);
        }
        List<HeatingData> data = dcsTdRealTimeDataMapper.heatingTrendChart(dcsRealTimeDataReq);
        //解析
        HeatingTrendChart heatingTrendChart = new HeatingTrendChart();
        if (CollectionUtils.isNotEmpty(data)) {
            // collectTime  采集时间
            List<String> collectTime = data.stream().map(HeatingData::getCollectTime).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setCollectTime(collectTime);
            // tr01
            List<Double> tr01 = data.stream().map(HeatingData::getTr01).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr01(tr01);
            // il3bb
            List<Double> il3bb = data.stream().map(HeatingData::getIl3bb).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setIl3bb(il3bb);
            // tr02
            List<Double> tr02 = data.stream().map(HeatingData::getTr02).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr02(tr02);
            // f1l4bb
            List<Double> f1l4bb = data.stream().map(HeatingData::getF1l4bb).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setF1l4bb(f1l4bb);
            // tr03
            List<Double> tr03 = data.stream().map(HeatingData::getTr03).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr03(tr03);
            // f2l4bb
            List<Double> f2l4bb = data.stream().map(HeatingData::getF2l4bb).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setF2l4bb(f2l4bb);
            // tr04
            List<Double> tr04 = data.stream().map(HeatingData::getTr04).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr04(tr04);
            // f3l4bb
            List<Double> f3l4bb = data.stream().map(HeatingData::getF3l4bb).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setF3l4bb(f3l4bb);
            // tr05
            List<Double> tr05 = data.stream().map(HeatingData::getTr05).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr05(tr05);
            // f4l1b
            List<Double> f4l1b = data.stream().map(HeatingData::getF4l1b).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setF4l1b(f4l1b);
            // tr06
            List<Double> tr06 = data.stream().map(HeatingData::getTr06).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr06(tr06);
            // d1l1br
            List<Double> d1l1br = data.stream().map(HeatingData::getD1l1br).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setD1l1br(d1l1br);
            // tr07
            List<Double> tr07 = data.stream().map(HeatingData::getTr07).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr07(tr07);
            // d2l1s
            List<Double> d2l1s = data.stream().map(HeatingData::getD2l1s).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setD2l1s(d2l1s);
            // tr08
            List<Double> tr08 = data.stream().map(HeatingData::getTr08).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr08(tr08);
            // d3l1s
            List<Double> d3l1s = data.stream().map(HeatingData::getD3l1s).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setD3l1s(d3l1s);
            // tr09
            List<Double> tr09 = data.stream().map(HeatingData::getTr09).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setTr09(tr09);
            // s4s
            List<Double> s4s = data.stream().map(HeatingData::getS4s).filter(Objects::nonNull).collect(Collectors.toList());
            heatingTrendChart.setS4s(s4s);
            // total  总功率
            List<XzTotalPower> totalPower = dcsTdRealTimeDataMapper.getTotalPower(dcsRealTimeDataReq);
            if (CollectionUtils.isNotEmpty(totalPower)) {
                List<String> totalCollectTime = totalPower.stream().map(XzTotalPower::getCollectTime).filter(Objects::nonNull).collect(Collectors.toList());
                heatingTrendChart.setTotalCollectTime(totalCollectTime);
                List<Double> totalPowerData = totalPower.stream().map(XzTotalPower::getData).filter(Objects::nonNull).collect(Collectors.toList());
                heatingTrendChart.setTotal(totalPowerData);
            }
        }
        return heatingTrendChart;
    }


    @Nullable
    private void setDefaultTime(DcsRealTimeDataReq dcsRealTimeDataReq) {
        if (dcsRealTimeDataReq.getStartTime() == null || dcsRealTimeDataReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            dcsRealTimeDataReq.setStartTime(startTime);
            dcsRealTimeDataReq.setEndTime(endTime);
        }
    }
}



